import {ElNotification} from 'element-plus'

// 自动关闭定时器
let timer = null
const closeMsg = () => {
  timer=null
}

/**
 * 倒计时 Message 方法
 */
const coutdownTime = (msgObj, className,  message, number,messageRight) => {
  timer = setTimeout(() => {
    if (number > -1&&timer) {
      msgObj.message =`${number}s${messageRight}` // element-ui 可起效，element-plus 不起效

      const parentDom = document.getElementsByClassName(className)[0]
      const childDom = parentDom.querySelectorAll('p')[0]
      childDom.innerHTML =`${number}s${messageRight}`
      --number
      coutdownTime(msgObj, className, message, number,messageRight)
    } else {
      msgObj.close()
      timer = null
    }
  }, 1000)
}

/**
 * 自动关闭 Message 方法
 */
function handleAutoCloseMessage(message, type, number, showClose,messageRight,fastVideoFun) {
  const randomNum = Math.floor(Math.random() * 10000)
  const className = `el-msg__${randomNum}`


  const msgObj = ElNotification({
    title: message,
    message: `${number}s${messageRight}`,
    duration: 0,
    showClose: showClose,
    customClass: className,
    onClose: () => {
      fastVideoFun()
      timer = null
    }
  })
  number--
  coutdownTime(msgObj, className, message, number,messageRight)
}

export {
  handleAutoCloseMessage,
  closeMsg
}
